import { createRouter, createWebHistory } from 'vue-router'
import { useMetaStore, useParamStorage } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
		{
			path: '/',
			name: 'home',
			component: () => import('../views/home'),
			meta: {
					title: '首页'
			},
			children: [
				{
					path: '/',
					name: 'index',
					component: () => import('@/views/index'),
					meta: {
						title: '首页'
					}
				},
				{
					path: '/admin/list',
					name: 'AdminList',
					component: () => import('@/views/admin/list'),
					meta: {
						title: '管理员列表'
					}
				},
				{
					path: '/grade/list',
					name: 'GradeList',
					component: () => import('@/views/grade/list'),
					meta: {
						title: '班级列表'
					}
				},
				{
					path: '/student/list',
					name: 'StudentList',
					component: () => import('@/views/student/list'),
					meta: {
						title: '学生列表'
					}
				},
				{
					path: '/student/import',
					name: 'StudentImport',
					component: () => import('@/views/student/import'),
					meta: {
						title: '导入学生列表'
					}
				},
				{
					path: '/question/list',
					name: 'QuestionList',
					component: () => import('@/views/question/list'),
					meta: {
						title: '题库列表'
					}
				},
				{
					path: '/question/import',
					name: 'QuestionImport',
					component: () => import('@/views/question/import'),
					meta: {
						title: '导入题库列表'
					}
				}
			]
		},
		{
			path: '/question/help',
			name: 'QuestionHelp',
			component: () => import('@/views/question/import/help'),
			meta: {
				title: '题库帮助文档'
			}
		},
		{
			path: '/error/404',
			name: 'error404',
			component: () => import('@/views/error/notfound'),
			meta: {
				title: '网页找不到'
			}
		},
		{
			path: '/:pathMatch(.*)*',
			redirect: '/error/404'
		}
	]
})
router.beforeEach((to, from ,next) => {
	if (to.name !== 'error404') {
		const metaStore = useMetaStore()
		document.title = to.meta.title
		metaStore.push({
			key: to.path,
			title: to.meta.title
		})
	}
	next()
}) 
export default router
